﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernateDB;

// .dll name has to be the same as the namespace
namespace BPFilter
{
    // Blood Pressure filter
    public class Filter : IFilter
    {
        private const int FILTER_ID = 1003;
        private const string FILTER_VER = "1.0";
        private const string FILTER_DESC =
            "Blood Pressure filter.\n" +
            "This filter is used to transfer files.";

        // variable used for BP calculations
        private double sumSys = 0;
        private double sumDia = 0;
        private int countSys = 0;
        private int countDia = 0;
        private double minSys = 0;
        private double maxSys = 0;
        private double minDia = 0;
        private double maxDia = 0;

        // server functions
        private ServerASISTSYS.ServerFunctions sf;


        #region IFilter Members

        public int GetID()
        {
            return FILTER_ID;
        }

        public string GetVersion()
        {
            return FILTER_VER;
        }

        public string GetDescription()
        {
            return FILTER_DESC;
        }

        public void Parse(int length, int id, byte[] data)
        {
            try
            {
                string str = "";
                string Date = "";
                string Hour = "";
                double ValueSys = 0;
                double ValueDia = 0;

                for (int i = 0; i < length; i++)
                {
                    str = str + (char)data[i];
                }

                Console.WriteLine("Data from BP sensor: " + str);

                if (str.Length > 0)
                {
                    ValueSys = Double.Parse(str.Substring(0, 5).Trim());
                    ValueDia = Double.Parse(str.Substring(5, 6).Trim());
                    Date = str.Substring(11, 11).Trim();
                    Hour = str.Substring(22, 8).Trim();
                }

                //Console.WriteLine("ValueSys: " + ValueSys);
                //Console.WriteLine("ValueDia: " + ValueDia);
                //Console.WriteLine("Date : " + Date);
                //Console.WriteLine("Hour : " + Hour);

                //BP caluclations
                //Systolic pressure calculations
                // min Systolic     
                if (ValueSys < 110)
                {
                    minSys = ValueSys;
                    sf.SendAlertMessage(sf.CNP, id, "MIN SYS " + minSys);
                }
                // max Systolic
                if (ValueSys > 130)
                {
                    maxSys = ValueSys;
                    sf.SendAlertMessage(sf.CNP, id, "MAX SYS " + maxSys);
                }
                // average Systolic
                sumSys = sumSys + ValueSys;
                countSys++;
                double tempMed = sumSys / countSys;

                //Console.WriteLine("minSys : " + minSys);
                //Console.WriteLine("maxSys : " + maxSys);
                //Console.WriteLine("SysMed : " + tempMed);

                // reset sensors minimum and maximum values.
                if (minSys > 0)
                {
                    //Console.WriteLine("Valoarea minima Sys: " + minSys);
                    minSys = 0;
                }
                if (maxSys > 0)
                {
                    //Console.WriteLine("Valoarea maxima Sys: " + maxSys);
                    maxSys = 0;
                }

                //Diastolic pressure calculations
                // min Diastolic     
                if (ValueDia < 75)
                {
                    minDia = ValueDia;
                    sf.SendAlertMessage(sf.CNP, id, "MIN DIA " + minDia);
                }
                // max Diastolic
                if (ValueDia > 85)
                {
                    maxDia = ValueDia;
                    sf.SendAlertMessage(sf.CNP, id, "MAX DIA " + maxDia);
                }
                // average Diastolic
                sumDia = sumDia + ValueDia;
                countDia++;
                double tempMed1 = sumDia / countDia;

                //Console.WriteLine("minDia : " + minDia);
                //Console.WriteLine("maxDia : " + maxDia);
                //Console.WriteLine("DiaMed : " + tempMed1);

                // reset sensors minimum and maximum values.
                if (minDia > 0)
                {
                    //Console.WriteLine("Valoarea minima Dia: " + minDia);
                    minDia = 0;
                }
                if (maxDia > 0)
                {
                    //Console.WriteLine("Valoarea maxima Dia: " + maxDia);
                    maxDia = 0;
                }

                // adaugare la BD
                SenzoriBP sbp = new SenzoriBP();

                sbp.IdSenzor = id;
                sbp.IdPacient = sf.CNP;
                sbp.Sys = ValueSys;
                sbp.Dia = ValueDia;
                sbp.Data = DateTime.Parse(Date + " " + Hour);

                PersistenceManager pm = PersistenceManager.Instance;
                pm.Save(sbp);
            }
            catch (FormatException)
            {
                // the message could not be processed
                Console.WriteLine("The format of the string is incorect!");
            }
        }

        public bool Validate()
        {
            return true;
            //throw new NotImplementedException();
        }

        public void SetServerFunctions(object sf)
        {
            this.sf = (ServerASISTSYS.ServerFunctions)sf;
        }

        public bool CanProcess(int id)
        {
            switch (id)
            {
                case 400001:
                    return true;
                default:
                    return false;
            }
        }

        #endregion
    }
}
